package org.example.resourceplatform.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDateTime;

@Entity
@Table(name = "download_history",
        indexes = {
                @Index(name = "idx_download_user", columnList = "user_id"),
                @Index(name = "idx_download_doc", columnList = "doc_id"),
                @Index(name = "idx_download_time", columnList = "download_time")
        })
@Getter
@Setter
@NoArgsConstructor
public class DownloadHistory {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "download_id")
    private Integer downloadId;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "doc_id", nullable = false)
    private Document document;

    @CreationTimestamp
    @Column(name = "download_time", nullable = false, updatable = false)
    private LocalDateTime downloadTime;

    @Column(name = "ip_address", length = 45)
    private String ipAddress;

    @Column(name = "user_agent", length = 255)
    private String userAgent;

    public DownloadHistory(User user, Document document, String ipAddress, String userAgent) {
        this.user = user;
        this.document = document;
        this.ipAddress = ipAddress;
        this.userAgent = userAgent;
    }

    @Override
    public String toString() {
        return "DownloadHistory{" +
                "downloadId=" + downloadId +
                ", userId=" + (user != null ? user.getUserId() : null) +
                ", documentId=" + (document != null ? document.getDocId() : null) +
                ", downloadTime=" + downloadTime +
                '}';
    }
}